package com.aysafety.iotdataplatformweb.controller.comprehensiveWarning;

import com.aysafety.core.common.CheckUtils;
import com.aysafety.core.common.DataConversionUtil;
import com.aysafety.core.common.DateUtils;
import com.aysafety.iotdataplatformweb.controller.base.BaseController;
import com.aysafety.iotdataplatformweb.services.comprehensiveWarning.ComprehensiveWarningService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: iotDataPlatform
 * @description: 综合预警
 * @author: ChengQi
 * @create: 2019-01-02 13:19
 **/

@RestController @RequestMapping("/api/comprehensiveWarning") public class DailyComprehensiveWarning
    extends BaseController {

    @Autowired ComprehensiveWarningService comprehensiveWarningService;
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    /**
     * @Description:离散度
     * @Param: [params, request, response]
     * @return: java.util.Map<java.lang.String,java.lang.Object>
     * @Author: ChengQi
     * @Date: 2019/3/13
     */
    @RequestMapping("/comprehensiveWarningMicroseism")
    public Map<String, Object> comprehensiveWarningMicroseism(
            @RequestParam Map<String, Object> params, HttpServletRequest request,
        HttpServletResponse response) {
        try {
            Map<String, Object> data = new HashMap<String, Object>();
            // 矿区id
            String mineId = DataConversionUtil.getStrFromMap("mineId", params);
            // 开始时间
            String startTime = DataConversionUtil.getStrFromMap("startTime", params);
            // 矿区id、工作面必传
            if (StringUtils.isBlank(mineId) || StringUtils.isBlank(startTime)) {
                return this.formatReturnData("缺少参数");
            }
            if (!CheckUtils.isValidDate(startTime, "yyyy-MM-dd HH:mm:ss")) {
                return this.formatReturnData("日期格式不正确");
            }
            // 结束时间
            String defaultEndTime = DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss");
            String endTime = DataConversionUtil.getStrFromMapOrSetDefaultValue("endTime", defaultEndTime, params);

            Map<String, Object> results = comprehensiveWarningService.comprehensiveWarningMicroseism(mineId, startTime, endTime);
            data.put("list", results);
            return this.formatReturnData(data);
        } catch (Exception e) {
            logger.error("comprehensiveWarningMicroseism:", e);
            return this.formatReturnData(e.getMessage());
        } catch (Throwable throwable) {
            logger.error("comprehensiveWarningMicroseism:", throwable);
            return this.formatReturnData(throwable.getMessage());
        }
    }

    /**
    * @Description: 地音综合预警
    * @Param: [params, request, response]
    * @return: java.util.Map<java.lang.String,java.lang.Object>
    * @Author: ChengQi
    * @Date: 2019/3/14
    */
    @RequestMapping("/comprehensiveWarningGeosound")
    public Map<String, Object> comprehensiveWarningGeosound(@RequestParam Map<String, Object> params, HttpServletRequest request,
        HttpServletResponse response) {
        try {
            Map<String, Object> data = new HashMap<String, Object>();
            // 矿区id
            String mineId = DataConversionUtil.getStrFromMap("mineId", params);
            // 开始时间
            String startTime = DataConversionUtil.getStrFromMap("startTime", params);
            // 矿区id、工作面必传
            if (StringUtils.isBlank(mineId) || StringUtils.isBlank(startTime)) {
                return this.formatReturnData("缺少参数");
            }
            if (!CheckUtils.isValidDate(startTime, "yyyy-MM-dd HH:mm:ss")) {
                return this.formatReturnData("日期格式不正确");
            }
            // 结束时间
            String defaultEndTime = DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss");
            String endTime = DataConversionUtil.getStrFromMapOrSetDefaultValue("endTime", defaultEndTime, params);

            Map<String, Object> results = comprehensiveWarningService.comprehensiveWarningGeosound(mineId, startTime, endTime);
            data.put("list", results);
            return this.formatReturnData(data);
        } catch (Exception e) {
            logger.error("comprehensiveWarningMicroseism:", e);
            return this.formatReturnData(e.getMessage());
        } catch (Throwable throwable) {
            logger.error("comprehensiveWarningMicroseism:", throwable);
            return this.formatReturnData(throwable.getMessage());
        }
    }

    /**
     * 液压支架综合预警学习
     * @param params
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/comprehensiveWarningHy")
    public Map<String, Object> comprehensiveWarningHy(@RequestParam Map<String, Object> params, HttpServletRequest request,
                                                            HttpServletResponse response) {
        try {
            Map<String, Object> data = new HashMap<String, Object>();
            // 矿区id
            String mineId = DataConversionUtil.getStrFromMap("mineId", params);
            // 开始时间
            String startTime = DataConversionUtil.getStrFromMap("startTime", params);
            // 矿区id、工作面必传
            if (StringUtils.isBlank(mineId) || StringUtils.isBlank(startTime)) {
                return this.formatReturnData("缺少参数");
            }
            if (!CheckUtils.isValidDate(startTime, "yyyy-MM-dd HH:mm:ss")) {
                return this.formatReturnData("日期格式不正确");
            }
            // 结束时间
            String defaultEndTime = DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss");
            String endTime = DataConversionUtil.getStrFromMapOrSetDefaultValue("endTime", defaultEndTime, params);

            Map<String, Object> results = comprehensiveWarningService.comprehensiveWarningHy(mineId, startTime, endTime);
            data.put("list", results);
            return this.formatReturnData(data);
        } catch (Exception e) {
            logger.error("comprehensiveWarningHy:", e);
            return this.formatReturnData(e.getMessage());
        } catch (Throwable throwable) {
            logger.error("comprehensiveWarningHy:", throwable);
            return this.formatReturnData(throwable.getMessage());
        }
    }

}
